package com.example.springbootServiceNetwork.demos.web.Service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.springbootServiceNetwork.demos.web.Config.Result;
import com.example.springbootServiceNetwork.demos.web.Service.WeiXinService;

import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class WeiXinServiceImpI implements WeiXinService {

    @Value("${weixin.appid}")
    private String appid;
    @Value("${weixin.secret}")
    private String secret;
    @Value("${weixin.wxUrl}")
    private String wxUrl;
    //获取token调用凭证
    public String WxToken() throws IOException{
        String access_token =null;
        //请求接口
        String comUrl = wxUrl+"cgi-bin/token?";
        //appid
        String appidStr = "&appid="+appid;
        //密匙
        String secretStr = "&secret="+secret;
        //小程序请求类型
        String grant_type = "&grant_type=client_credential";
        //校验缓存里面的token是否存在或者过期

        if (false){
            System.out.println("token");
        }else {
            //完整请求地址
            String url=comUrl+appidStr+secretStr+grant_type;
            //请求
            OkHttpClient client =new OkHttpClient();

            Request request =new Request.Builder().url(url).build();
            Response response =client.newCall(request).execute();
            //判断是否接收到返回信息
            if(response.isSuccessful()){
                String body = null;
                if (response.body() != null) {
                    body = response.body().string();
                }
                JSONObject jsonObj = JSON.parseObject(body);
                access_token=jsonObj.getString("access_token");
            }
        }
        return access_token;
    }

    @Override //获取微信小程序手机号码
    public Result PhoneNumber(String code) throws IOException{
        //初始化返回格式
        Result result =new Result();
        //请求接口
        String comUrl = wxUrl+"wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN";

        //请求
        OkHttpClient client =new OkHttpClient();
        // 创建RequestBody对象
        RequestBody requestBody = new FormBody.Builder()
                .add("access_token", WxToken())
                .add("code", code)
                .build();

        // 创建Request对象并设置请求方法为POST
        Request request = new Request.Builder()
                .url(comUrl)
                .post(requestBody)
                .build();

        Response response =client.newCall(request).execute();
        //判断是否接收到返回信息
        if(response.isSuccessful()){
            String body = null;
            if (response.body() != null) {
                body = response.body().string();
            }
            JSONObject jsonObj = JSON.parseObject(body);
            result.setData(jsonObj);
            result.setCode(200);
        }else {
            result.setCode(400);
            result.setMsg("信息错误");
        }
        return result;
    }

}
